תזכורת: עץבינארי מבוא למדעי המחשב הרצאה 24: עצי חיפוש בינאריים

Similar documents
עצים. מבני נתונים Iterators רשימות מקושרות עצים "רגילות" רקורסיביות

תור שימושים בעולם התוכנה

מבוא למדעי המחשב תרגול 8 רשימה משורשרת כללית, Comparator

מבוא למדעי המחשב תרגול 10 הממשקים Iterator, Iterable Binary trees

מבוא למדעי המחשב 2018 תרגול 7

תרגול 7 רשימות משורשרות, רקורסיית

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

מבני נתונים תכנות מונחה עצמים מבני נתונים. מחלקות אבסטרקטיות חבילות packages סיכום הרשאות גישה wrappers ADT מערך דינמי מחסנית

רזח יליגרתו םי יראני ב ם

Figure 18.4 A Unix directory. 02/10/04 Lecture 9 1

מבוא למדעי המחשב תרגול 13: עצים בינאריים

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

Generic BST Interface

מערכים שעור מס. 4 כל הזכויות שמורות דר ' דרור טובי המרכז האוניברסיטאי אריאל 1

Recursion. Example 1: Fibonacci Numbers 1, 2, 3, 5, 8, 13, 21, 34, 55, 89,

מבוא למדעי המחשב תרגול 10 Comparator, Comparable, Binary Trees

מבוא לתכנות ב- JAVA תרגול 7

JAVA NOTES DATA STRUCTURES

Review: Trees Binary Search Trees Sets in Java Collections API CS1102S: Data Structures and Algorithms 05 A: Trees II

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

תוכנה 1 סמסטר א' תשע"א

Practical Session No. 14 Topological sort,amortized Analysis

void insert( Type const & ) void push_front( Type const & )

Binary Node. private Object element; private BinaryNode left; private BinaryNode right; 02/18/03 Lecture 12 1

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

Figure 18.4 A Unix directory. 02/13/03 Lecture 11 1

הנכות 1 תואיגש םע תודדומתהו תואלול,םי : כרעמ 2 לוגרת

CMSC 341. Binary Search Trees CMSC 341 BST

CIS 121. Binary Search Trees

protected BinaryNode root; } 02/17/04 Lecture 11 1

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

The Problem with Linked Lists. Topic 18. Attendance Question 1. Binary Search Trees. -Monty Python and The Holy Grail

ITI Introduction to Computing II

Data Structures in Java

Lecture Topics. Object Structures. Hierarchical Organization. Tree Concepts. Hierarchical Organization. Hierarchical Organization

תוכנה 1. תרגול מספר 11: Static vs. Dynamic Binding מחלקות מקוננות Nested Classes

תוכנה 1 תרגול 2: מערכים ומבני בקרה

ITI Introduction to Computing II

הנכות 1 תואיגש םע תודדומתהו תואלול,םיכרעמ : לו 2 גרת

Practical Session - Heap

ת ונכת סרוק תורשוקמ תומישר :יעישת רועיש 1

רשימות דילוגים Skip Lists

קורס תכנות רשימה מקושרת דוגמה: חיפוש מעבר על רשימה דוגמא: שחרור רשימה מקושרת דוגמא: הוספת אברים שלא בהתחלה

Motivation Computer Information Systems Storage Retrieval Updates. Binary Search Trees. OrderedStructures. Binary Search Tree

לתיכנות עם MATLAB Lecture 5: Boolean logic and Boolean expressions

IMPLEMENTING BINARY TREES

Lecture 27. Binary Search Trees. Binary Search Trees

CMSC 341 Lecture 10 Binary Search Trees

CS 315 Data Structures mid-term 2

תרגול 3 מערכים ופונקציות

a graph is a data structure made up of nodes in graph theory the links are normally called edges

Programming II (CS300)

/department of mathematics and computer science 1/58

Practical Session #4 - ADTs: Array, Queue, Stack, Linked List

הפלט אחרי הביצוע של ההוראה :what3(root)

INFO1x05 Tutorial 6. Exercise 1: Heaps and Priority Queues

תכנות מונחה עצמים משחקים תשע"ו

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

Computer Science II Fall 2009

Priority Queues. 04/10/03 Lecture 22 1

Birkbeck (University of London) Software and Programming 1 In-class Test Mar Answer ALL Questions

COM S 211/ENGRD 211 May 15, 2003

CS302 - Data Structures using C++

חומר עזר לבחינה במבוא למדעי המחשב // Indicates whether some other object is "equal to" // this one. boolean equals(object other)

Name Section Number. CS210 Exam #3 *** PLEASE TURN OFF ALL CELL PHONES*** Practice

A grossly unbalanced tree, with some long paths. Next to add: When does it occur? Why is it undesirable?

4/18/ Binary Search Trees (BSTs) 17.1 Adding an Element to a BST Removing an Element from a BST. 17.

9. טופס הזמנת מוצרים טופס ההזמנה הוא טופס מורכב. מעורבים בו 4 טבלאות נתונים. קשרי הגומלין בין הטבלאות : הטופס :

AP Programming - Chapter 20 Lecture page 1 of 17

Comp Intermediate Programming EXAM #2 Supplement April 02, 2003 Rice University - Instructors: Cox & Nguyen

COMP : Trees. COMP20012 Trees 219

Priority Queues. INFO0902 Data Structures and Algorithms. Priority Queues (files à priorités) Keys. Priority Queues

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

סכום (סדרת ערכים) אחרת - דוגמא: סכום-ספרות (num) אם < 10 num החזר 1 או אם = 0 = num החזר 0 public static int numofdigits (int num)

1.00 Introduction to Computers and Engineering Problem Solving. Final Examination - May 19, 2004

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CS 151. Binary Search Trees. Wednesday, October 10, 12

: Advanced Programming Final Exam Summer 2008 June 27, 2008

רשימות דילוגים Skip Lists

CS302 - Data Structures using C++

Name (First and Last): SAMPLE SOLUTIONS

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Loop through Arrays. Array Creation and Initialization

selectors, methodsinsert() andto_string() the depth of a tree and a membership function

4. Trees. 4.1 Preliminaries. 4.2 Binary trees. 4.3 Binary search trees. 4.4 AVL trees. 4.5 Splay trees. 4.6 B-trees. 4. Trees

Chapter 20: Binary Trees

Chapter 11.2 Linked lists ( )

תוכנה 1 מערכים. Array Creation and Initialization. Array Declaration. Array Creation and Initialization. Loop through Arrays

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

תוכנה 1 3 תרגול מס' מערכים ומבני בקרה

Points off A 4B 5 Total off Net Score. CS 314 Final Exam Spring 2015

Problems with simple variables

תוכנה 1 תרגול 2: מערכים, מבני בקרה ושגיאות

You have seen abstractions in many places, lets consider them from the ground up.

הנכות 1 םוכיס לוגרת 14 1

ב ה צ ל ח ה! אוניברסיטת בן גוריון בנגב מספר נבחן : תאריך המבחן: כ"ה תשרי תשע"ח 15/10/17 שמות המורים: ציון סיקסיק מיועד לתלמידי : א'

מבוא למדעי המחשב השפעת השינוי על סטודנט הרצאה 18: פולימורפיזם ומחלקות אבסטרקטיות אם ברצוננו ששכר הלימוד לא יעלה על 2500.

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211

1.00/1.001 Introduction to Computers and Engineering Problem Solving. Final Exam

תוכנה 1 בשפת Java נושאים שונים בהורשה רובי בוים ומתי שמרת בית הספר למדעי המחשב אוניברסיטת תל אביב

Algorithms. Intro2CS week 5

Transcription:

מבוא למדעי המחשב הרצאה 2: עצי חיפוש בינאריים תזכורת: עץבינארי בנוסףלרשימהמקושרת ומערך, הצגנומבנהנתונים קונקרטיחדש עץבינארי עץבינארימורכבמ: שורש תת-עץשמאלי תת-עץימני A B C D E F G 2 תזכורת: שורש ותתי-עצים left subtree root right subtree עץ בינארי לא ריק ניתן לחלק לשורש ולשני תתי-עצים משימה בסיסית של מחשב נרצהלנהלקבוצת איבריםסדורה, כך שניתן יהיה להוסיף, לחפש, למצוא איבר מינימאלי ולהוציא איבר בזמן מהיר איזה מבנה נתונים הכי מתאים לביצוע המשימה? מערך ממוין? רשימה מקושרת? עץ בינארי? זמני ריצה עץחיפושבינאריהואעץחיפושאשרבסריקת InOrder מוצגיםאיבריולפיהסדרהנכון 20 6 עץ בינארי לעומת עץ חיפוש בינארי מערכיםממוינים : מציאתמנימום: מעברעלאיבריחיד חיפוש: מעברעללכלהיותר log n איברים הכנסה: מעברעללכלהיותר n איברים רשימות מקושרותלאממוינות \ ממוינות: מציאתמנימום: מעברעל n איבריםלכלהיותר \ איבריחיד חיפוש: מעברעל n איבריםלכלהיותר (בשניהמקרים) הכנסה: מעברעלאיבריחיד \ n איבריםלכלהיותר עציחיפושבינאריים (בגובהh ): מציאתמנימום: מעברעל h איבריםלכלהיותר חיפוש: מעברעל h איבריםלכלהיותר הכנסה: מעברעל h איבריםלכלהיותר

הרעיון של עץ חיפוש בינארי דוגמאות לעץ חיפוש בינארי 2 2 כלהאיבריםבתתהעץהשמאליקטנים מהאיברבשורש, וכלהאיבריםבתתהעץ הימניגדוליםמהאיברבשורש תנאיםאלומתקיימיםגםביחסלשורשהעץ כולו, וגםבכלאחדמתתיהעציםשלהעץ זהלאעץחיפושבינארי עציחיפושבינאריים 8 הכנסת איבר לעץ חיפוש בינארי דוגמת בנייה של עץ חיפוש בינארי כאשרמוכנסאיבר, ערכומושווהעםהשורש אםערכוקטןמערךהשורש, הואמוכנס באופןרקורסיבילתתהעץהשמאלי אםערכוגדולמערךהשורש, הואמוכנס באופןרקורסיבילתתהעץהימני במקרהשלשוויון, בחרנובמימושזהלא להכניסאתהאיבר נניח כי נרצה להכניס את האיברים הבאים לעץ חיפוש בינארי:,,,,, 8, 9 דוגמת בנייה של עץ חיפוש בינארי נניח כי נרצה להכניס את האיברים הבאים לעץ חיפוש בינארי:,,,,, 8, איך נממש? Binary Search Trees האםניתןלהשתמשבקודשל BinaryTree ואם כן איך? נכון! ע"יהורשה 8 8 2 2

כיצד נשווה בין איברי העץ? עלמנתלהחזיקאתהאיבריםבעץ באופןממויןעלינולדאוג שאיבריםיהיובעלי השוואהלכןנשתמשבממשק - Comparable (תזכורתלממשק): המחלקהBinarySearchNode public class BinarySearchNode extends BinaryNode { public BinarySearchNode (Comparable data) {super(data); public void insert(object toadd) { if (!(toadd instanceof Comparable)) throw new RuntimeException("Only a Comparable data may be inserted!"); else insert((comparable) toadd); public boolean contains(object tofind) { if (!(tofind instanceof Comparable)) return false; else return contains((comparable) tofind); public interface Comparable { public int compareto(object other); המחלקהBinarySearchNode המחלקהBinarySearchNode private boolean contains(comparable tofind) { int compareresult = tofind.compareto(data); if (compareresult == 0) return true; else if (compareresult < 0){ return (left!= null && left.contains(tofind)); else return (right!= null && right.contains(tofind)); private void insert(comparable toadd) { if (toadd.compareto(data) < 0){ if (left == null) left = new BinarySearchNode (toadd); else left.insert(toadd); else if (toadd.compareto(data) > 0){ if (right == null) right = new BinarySearchNode (toadd); else right.insert(toadd); הדגמהשלפעולת contains הדגמהשלפעולת contains 8 < go left 8

הדגמהשלפעולת contains הדגמהשלפעולת contains 8 > go right 8 > go right But right == null return false 20 9 מציאת מינימום בעץ מחיקת איבר מהעץ נרצהלהוציאאיברכלשהומהעץ (אםהאיבר נמצאבעץ) נטפלבשלושהמקרים: מקרהראשון: האיברנמצאבעלה מקרהשני: לצומתשלהאיברישבןאחד מקרהשלישי: לצומתשלהאיבריששניבנים public Object findmin(){ BinaryNode currnode = this; while (currnode.left!= null){ currnode = currnode.left; return currnode.data; 22 2 מחיקתאיברמהעץ מקרה ראשון מחיקתאיברמהעץ מקרה שני לצומתהמכילאתהאיברישבןאחד נניחשרוציםלהורידאתהאיבר הצומתהמכילאתהאיברהואעלה נניחשרוציםלהורידאתהאיבר פשוטנהפוךאתהמצביעשלהאבשל להיות null פשוט "נעלה" אתתתהעץשלהבןהיחידשל 2 2

public BinarySearchNode remove(comparable toremove){ if (toremove.compareto(data) < 0){ if (left!= null) left = ((BSN) left).remove(toremove); else if (toremove.compareto(data) > 0){ if (right!= null) right = ((BSN) right).remove(toremove); else{//need to remove the data in this node if (left == null right == null){ // the base cases... if (left == null) return right; else return left; else{ // this node has two children data = ((BSN) right).findmin(); right = ((BSN) right).remove((comparable) data); return this; //remove 26 מחיקתאיברמהעץ מקרהשלישי לצומתהמכילאתהאיבריששניבנים נניחשרוציםלהורידאתהאיבר נעתיקאתהמידעמהאיברהמינימאליבתת-העץ הימנישלהצומתהמכילאת () 20 נמחקאתהאיברהמינימאלי בתתהעץהימני, באופן רקורסיבי 2 public interface Comparator { int compare(object obj, Object obj2); // comparator 28 לעיתיםאנומעונייניםלהשוותביןשניאוביקטים לפיכמההשוואותנפרדות, לדוגמהסטודנטיםעפ"י מספרת.ז. שםמשפחה ציונים מהנעשה? נצמיד "שופט" למחלקה שיודע "להכריע" מי מביןשניעצמיםמאותהמחלקה יותרגדול 2 public class StudentComparatorByID implements Comparator { public int compare(object obj, Object obj2){ double id= ((Student) o).getid(); double id2= ((Student) o2).getid(); return id - id2; // StudentComparatorByID public class StudentComparatorByName implements Comparator { public int compare(object obj, Object obj2){ String name= ((Student) o).getname(); String name2= ((Student) o2).getname(); return name.compareto(name2); // StudentComparatorByName 0 29

public class StudentComparatorByNumOfPoints implements Comparator { public int compare(object obj, Object obj2){ doube nump= ((Student) o).getpoints(); double nump2= ((Student) o2).getpoints(); return nump - nump2; // StudentComparatorByNumOfPoints public class StudentComparatorByGrade implements Comparator { public int compare(object obj, Object obj2){ double grade= ((Student) o).getavggrade(); double grade2= ((Student) o2).getavegrade(); return grade - grade2; // StudentComparatorByGrade 2 Back to Binary Search Trees איךנממש? האםניתןלהשתמשבקודשל BinaryTree I think I have a DeJaVu! ואםכןאיך? נכון! שובע"יהורשה public static void insertionsort(object [] arr, Comparator comp){ for(int i=; i < arr.length; i = i + ){ int j=i; while(j>0 && (comp.compare(arr[i], arr[j-])<0){ arr[j] = arr[j-]; j=j -; BinarySearchTree public class BinarySearchNode extends BinaryNode{ private Comparator comp; public BinarySearchNode(Object data, Comparator comp) { super(data); this.comp = comp; // BinarySearchNode // (override insert, remove, etc.) // class BinarySearchNode BinarySearchTree import java.util.comparator; public class BinarySearchTree extends BinaryTree{ private Comparator comp; public BinarySearchTree(Comparator comp){ super(); this.comp = comp; // BinarySearchTree 6 6

CharacterComparator IntegerComparator public class CharacterComparator implements Comparator { public int compare(object o, Object o2) { return ((Character)o).charValue() - ((Character)o2).charValue()) ; public class IntegerComparator implements Comparator { public int compare(object o, Object o2) { return ((Integer)o).intValue() - ((Integer)o2).intValue() 8 הכנסה איבר חדש לעץ במחלקה BinarySearchTree public void insert(object toadd){ if (isempty()) { root = new BSN(toAdd, this.comp); else { root.insert(toadd); // insert 0 Binary search Tree import java.util.comparator; public class Main { public static void main(string[] args) { Comparator comp = new IntegerComparator(); BinarySearchTree tree = new BinarySearchTree(comp); tree.insert(new Integer(2)); tree.insert(new Integer()); tree.insert(new Integer()); tree.insert(new Integer()); 9 הכנסה איבר חדש לעץ תכונות של עץ חיפוש בינארי ההכנסה\הוצאה היא מקומית, כלומר אין צורךלהזיז את כל שאר האיברים עץ חיפוש בינארי הוא עץ בינארי לכל דבר (מבחינתגובה, גודלוכו') הזמן הדרוש לביצוע הפעולות פרופורציוני לגובה העץ, ולא למספר האיברים בעץ (כפי שקורה במערכים ורשימות). כאשר העץ מאוזן, גובהובערך.logn 2 במחלקה BinarySearchNode public void insert(object toadd) { if (comp.compare(toadd, this.data) < 0) if (left == null) left = new BSN(toAdd,this.comp); else left.insert(toadd); if (comp.compare(toadd, this.data) > 0) { if (right == null) right = new BSN(toAdd,this.comp); else right.insert(toadd); // insert